﻿/**
* 动态数组
* @file dynamic_array.hpp
* @author Song
* @date 2011-10-13
*
*/

#pragma once
#ifndef DYNAMIC_ARRAY_HPP
#define DYNAMIC_ARRAY_HPP

#include <utility>
#include <vector>
#include <memory>
#include <cstring>

/// @addtogroup helpers_text
/// @{

namespace helpers {

	namespace text {
#if 0
		/**
		* 字节数组模板类;
		*/
		template <typename T>
		class basic_array
		{
		public:

			/**
			* 默认构造操作;
			* @return 无返回值;
			*/
			basic_array()
				: _data_ptr(_data)
				, _size(0)
			{
				_data_ptr[0] = _data_ptr[1] = _data_ptr[2] = _data_ptr[3] = 0;
			}

			/**
			* 分配构造操作;
			* @param [in] n 字节数组尺寸;
			* @return 无返回值;
			*/
			basic_array(size_t n)
				: _data_ptr(_data)
				, _size(0)
			{
				_data_ptr = n < 4 ? _data : new T[n];
				_size = n;
				_data_ptr[0] = _data_ptr[1] = _data_ptr[2] = _data_ptr[3] = 0;
			}

			/**
			* 复制构造操作;
			* @param [in] rhs 源字节数组对象;
			* @return 无返回值;
			*/
			basic_array(const basic_array<T>& rhs)
				: _data_ptr(_data)
				, _size(0)
			{
				_data_ptr = rhs._size < 4 ? _data :new T[rhs._size];
				_size = rhs._size;
				_data_ptr[0] = _data_ptr[1] = _data_ptr[2] = _data_ptr[3] = 0;
				std::memcpy(_data_ptr, rhs._data_ptr, rhs._size * sizeof(T));
			}

			/**
			* 析构操作;
			* @return 无返回值;
			*/
			~basic_array()
			{
				if (_data_ptr != _data)
					delete _data_ptr;
			}

		public:

			/**
			* 赋值复制操作;
			* @param [in] rhs 源字节数组对象;
			* @return 赋值后的字节数组对象;
			*/
			basic_array<T>& operator = (const basic_array<T>& rhs)
			{
				if (_data_ptr != _data)
					delete _data_ptr;
				_data_ptr = rhs._size < 4 ? _data : new T[rhs._size];
				_size = rhs._size;
				_data_ptr[0] = _data_ptr[1] = _data_ptr[2] = _data_ptr[3] = 0;
				std::memcpy(_data_ptr, rhs._data_ptr, rhs._size * sizeof(T));
				return *this;
			}

			size_t size() const
			{
				return _size;
			}

		public:
			/**
			* 获取字符数组指针;
			* @return 字符数组指针;
			*/
			operator char*() const
			{
				return reinterpret_cast<char *>(_data_ptr);
			}

			/**
			* 获取无符号字符数组指针;
			* @return 无符号字符数组指针;
			*/
			operator unsigned char*() const
			{
				return reinterpret_cast<unsigned char *>(_data_ptr);
			}

			/**
			* 获取宽字符数组指针;
			* @return 宽字符数组指针;
			*/
			operator wchar_t* () const
			{
				return reinterpret_cast<wchar_t *>(_data_ptr);
			}

			/// 短形(16bit)有符号数指针隐式转换操作符.
			/// 用于将对象内数据地址转换为短形有符号数指针
			/// @return 短形有符号数指针
			operator short*() const
			{
				return reinterpret_cast<short *>(_data_ptr);
			}

			/// 短形(16bit)无符号数指针隐式转换操作符.
			/// 用于将对象内数据地址转换为短形无符号数指针
			/// @return 短形无符号数指针
			operator unsigned short*() const
			{
				return reinterpret_cast<unsigned short *>(_data_ptr);
			}

			/// 整形有符号数指针隐式转换操作符.
			/// 用于将对象内数据地址转换为整形有符号数指针
			/// @return 整形有符号数指针
			operator int*() const
			{
				return reinterpret_cast<int *>(_data_ptr);
			}

			/// 整形无符号数指针隐式转换操作符.
			/// 用于将对象内数据地址转换为整形无符号数指针
			/// @return 整形无符号数指针
			operator unsigned int*() const
			{
				return reinterpret_cast<unsigned int *>(_data_ptr);
			}

			/**
			* 获取无类型数据指针;
			* @return 无类型数据指针;
			*/
			operator void* () const
			{
				return reinterpret_cast<void *>(_data_ptr);
			}

		private:
			T	_data[4]; ///< 临时数据缓冲区
			T*	_data_ptr; ///< 数据指针
			size_t _size; ///< 数据大小
		}; // class basic_array;
#endif








		/// 动态数组模版
		template <typename T>
		class dynamic_array {
		public:
			typedef T				value;
			typedef const T *const	const_pointer;
			typedef T *				pointer;
			typedef T&				reference;
			typedef const T&		const_reference;

		public:
			dynamic_array()
				: _data()
			{
			}

			/// 按元素个数初始化数组
			explicit dynamic_array(size_t size)
				: _data()
			{
				_data.resize(size);
			}

			dynamic_array(const dynamic_array& rhs)
				: _data(rhs._data)
			{
			}

			~dynamic_array()
			{

			}
		public:
			dynamic_array& operator=(dynamic_array& rhs)
			{
				dynamic_array(rhs).swap(*this);
				return *this;
			}

		public:
			/// 重新设置数组容纳元素个数
			/// @note 该函数无法减少空间大小，空间大小等于最大一次设置
			void resize(size_t size)
			{
				_data.resize(size, value());
			}

			void resize(size_t size, value val)
			{
				_data.resize(size, val);
			}

			/// 返回设置的元素个数
			size_t size() const
			{
				return _data.size();
			}

			/// 
			void swap(dynamic_array& rhs) 
			{
				_data.swap(rhs._data);
			}

			/// 返回指定位置元素引用，注意不能超过size()
			reference operator[](size_t pos)
			{
				if (pos >= size())
					throw std::exception("Out of range");

				return _data[pos];
			}

			const_reference operator[](size_t pos) const
			{
				if (pos >= size())
					throw std::exception("Out of range");
				return _data[pos];
			}

			/// 返回数组元素空间首地址
			pointer data()
			{
				return &_data[0];
			}

			const_pointer data() const
			{
				return &_data[0];
			}

			/// 返回
			operator pointer()
			{
				return data();
			}

			operator const_pointer() const 
			{
				return data();
			}

		protected:
			std::vector<value> _data;
		};

		/// 字节动态数组
		class ByteArray : public dynamic_array<char>
		{
		public:
			typedef std::shared_ptr<ByteArray> Ptr;

		public:
			ByteArray()
				: dynamic_array<char>()
			{

			}

			explicit ByteArray(size_t size)
				: dynamic_array<char>(size)
			{

			}
			
			explicit ByteArray(const char* rhs, int size)
				: dynamic_array<char>(size)
			{
				std::strncpy(data(), rhs, size);

			}
		public:
#if 0
			/**
			* 获取字符数组指针;
			* @return 字符数组指针;
			*/
			operator char_t*()
			{
				return reinterpret_cast<char_t*>(data());
			}

			/**
			* 获取字符数组指针;
			* @return 字符数组指针;
			*/
			operator const char_t*() const 
			{
				return reinterpret_cast<const char_t*>(data());
			}
#endif

			/**
			* 获取宽字符数组指针;
			* @return 宽字符数组指针;
			*/
			operator wchar_t* ()
			{
				return reinterpret_cast<wchar_t *>(data());
			}

			operator const wchar_t*() const 
			{
				return reinterpret_cast<const wchar_t*>(data());
			}
		};

		//	typedef basic_array<unsigned char_t> ByteArray; ///< 无符号单字节型数组

	} // namespace text;

} // namespace helpers

#endif

/// @}


/// @addtogroup helpers
/// @{
///		@defgroup helpers_text 文本字符串操作
///		包含了编码转换、动态数组、字符串格式化等实用工具
/// @}



/// @addtogroup helpers_text
/// 
///	@par 
/// @li @ref text::Unicode
/// @li text::ByteArray
/// 
/// 
/// 
